<?php
/**用户列表
 * Created by leiying
 * Date: 2018-02-25
 * Time: 16:57:44
 */

namespace Admin\Controller;


class UsersController extends AdminController
{
    /**列表
     * @author leiying
     */
    public function UsersList(){
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_UsersList',$get);
        }else{
            if(empty(I('get.p'))){
                session('searchdata_UsersList',null);
            }
            $get = $search_data = session('searchdata_UsersList');
        }
        //搜索条件：用户ID
        if(isset($get['user_id']) && $get['user_id'] != ''){
            $search_data['user_id'] = $get['user_id'];
            $where['a.user_id'] = array('eq',trim($search_data['user_id']));
        }

        //搜索条件：昵称
        if(isset($get['nickname']) && $get['nickname'] != ''){
            $search_data['nickname'] = trim($get['nickname']);
            $where['a.nickname'] = array('like','%'.$search_data['nickname'].'%');
        }

        //搜索条件：真实姓名
        if(isset($get['user_name']) && $get['user_name'] != ''){
            $search_data['user_name'] = trim($get['user_name']);
            $where['a.user_name'] = array('like','%'.$search_data['user_name'].'%');
        }

        //搜索条件：手机号
        if(isset($get['user_phone']) && $get['user_phone'] != ''){
            $search_data['user_phone'] = trim($get['user_phone']);
            $where['a.user_phone'] = array('like','%'.$search_data['user_phone'].'%');
        }

        if(isset($get['parent_id']) && $get['parent_id'] != ''){
            $search_data['parent_id'] = $get['parent_id'];
            $where['a.parent_id'] = array('eq',trim($search_data['parent_id']));
        }
        if(isset($get['status']) && $get['status'] != ''){
            $search_data['status'] = $get['status'];
            $where['a.status'] = array('eq',$search_data['status']);
        }

        $count = M('users')->alias("a")->where($where)->count();

        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        //处理排序逻辑(默认主键倒序)
        $sort = array(
            'field'=>I('get.sort_field','user_id'),
            'type'=>I('get.sort_type','desc'),
        );
        $sortString = 'a.'.$sort['field'] . ' ' .$sort['type'];
        $this->assign('sort',$sort);

        $lists = M('users')
            ->alias("a")
            ->field("a.*,b.nickname parent_name")
            ->join(C('DB_PREFIX')."users b ON a.parent_id = b.user_id",'left')
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order($sortString)
            ->select();

        $this->assign('lists',$lists);
        $this->assign('searchs',$search_data);
        $p = I('get.p',"1");
        $this->assign("p",$p);
        $this->display();
    }


    /**批量操作
     * @param null $method
     * @author leiying
     */
    public function changeStatus($method=null){
        $id = I('post.id');
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        switch ( strtolower($method) ){
            case 'deletegroup':
                $res = M("users")->where(array("user_id"=>array('in',$id)))->delete();
                if($res){
                    $this->success("删除成功");
                }else{
                    $this->error("删除失败");
                }
                break;
            case 'forbiduser':
                $res = M("users")->where(array("user_id"=>array('eq',$id)))->save(array('status'=>2));
                if($res){
                    $this->success("禁用成功");
                }else{
                    $this->error("禁用失败");
                }
                break;
            case 'resumeuser':
                $res = M("users")->where(array("user_id"=>array('eq',$id)))->save(array('status'=>1));
                if($res){
                    $this->success("启用成功");
                }else{
                    $this->error("启用失败");
                }
                break;
            default:
                $this->error($method.'参数非法');
        }
    }

    /**添加
     * @author leiying
     */
    public function UsersAdd(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            /*处理参数(请根据业务逻辑适当修改)*/
            //昵称
            $nickname = trim($post['nickname']);
            if(empty($nickname)){
                $this->error("昵称不能为空");
            }
            //真实姓名
            $user_name = trim($post['user_name']);
            if(empty($user_name)){
                $this->error("真实姓名不能为空");
            }
            //手机号
            $user_phone = trim($post['user_phone']);
            if(empty($user_phone)){
                $this->error("手机号不能为空");
            }
            //账号余额
            $user_balance = trim($post['user_balance']);
            if(empty($user_balance)){
                $this->error("账号余额不能为空");
            }
            //投资金额
            $user_investment = trim($post['user_investment']);
            if(empty($user_investment)){
                $this->error("投资金额不能为空");
            }
            //收益金额
            $user_earnings = trim($post['user_earnings']);
            if(empty($user_earnings)){
                $this->error("收益金额不能为空");
            }
            //冻结金额
            $frozen_money = trim($post['frozen_money']);
            if(empty($frozen_money)){
                $this->error("冻结金额不能为空");
            }
            //邀请人
            $parent_id = trim($post['parent_id']);
            if(empty($parent_id)){
                $this->error("邀请人不能为空");
            }
            //认证状态
            $is_validated = trim($post['is_validated']);
            if(empty($is_validated)){
                $this->error("认证状态不能为空");
            }
            //账号状态
            $status = trim($post['status']);
            if(empty($status)){
                $this->error("账号状态不能为空");
            }
            $data = array(
                'add_time'=>time(),
                'nickname'=>$nickname,
                'user_name'=>$user_name,
                'user_phone'=>$user_phone,
                'user_balance'=>$user_balance,
                'user_investment'=>$user_investment,
                'user_earnings'=>$user_earnings,
                'frozen_money'=>$frozen_money,
                'parent_id'=>$parent_id,
                'is_validated'=>$is_validated,
                'status'=>$status,
            );
            $res = M('users')->add($data);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->display();
        }
    }

    /**编辑
     * @author leiying
     */
    public function UsersEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            $id = $post['id'];
            if(empty($id)){
                $this->error('请选择需要操作的数据');
            }
            /*处理参数(请根据业务逻辑适当修改)*/
            //昵称
            $nickname = trim($post['nickname']);
            if(empty($nickname)){
                $this->error("昵称不能为空");
            }
            //真实姓名
            $user_name = trim($post['user_name']);
            if(empty($user_name)){
                $this->error("真实姓名不能为空");
            }
            //手机号
            $user_phone = trim($post['user_phone']);
            if(empty($user_phone)){
                $this->error("手机号不能为空");
            }
            //账号余额
            $user_balance = trim($post['user_balance']);
            if(empty($user_balance)){
                $this->error("账号余额不能为空");
            }
            //投资金额
            $user_investment = trim($post['user_investment']);
            if(empty($user_investment)){
                $this->error("投资金额不能为空");
            }
            //收益金额
            $user_earnings = trim($post['user_earnings']);
            if(empty($user_earnings)){
                $this->error("收益金额不能为空");
            }
            //冻结金额
            $frozen_money = trim($post['frozen_money']);
            if(empty($frozen_money)){
                $this->error("冻结金额不能为空");
            }
            //邀请人
            $parent_id = trim($post['parent_id']);
            if(empty($parent_id)){
                $this->error("邀请人不能为空");
            }
            //认证状态
            $is_validated = trim($post['is_validated']);
            if(empty($is_validated)){
                $this->error("认证状态不能为空");
            }
            //账号状态
            $status = trim($post['status']);
            if(empty($status)){
                $this->error("账号状态不能为空");
            }
            $data = array(
                'nickname'=>$nickname,
                'user_name'=>$user_name,
                'user_phone'=>$user_phone,
                'user_balance'=>$user_balance,
                'user_investment'=>$user_investment,
                'user_earnings'=>$user_earnings,
                'frozen_money'=>$frozen_money,
                'parent_id'=>$parent_id,
                'is_validated'=>$is_validated,
                'status'=>$status,
            );
            $res = M('users')->where(array("user_id"=>$id))->save($data);
            if(is_numeric($res)){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }else{
            $id = I('get.id');
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }
            $res = M('users')->where(array('user_id'=>$id))->find();
            $this->assign('res',$res);
            $p = I('get.p',"1");
            $this->assign('p',$p);
            $this->display();
        }
    }

}